package com.wangshili.sys.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wangshili.pojo.common.vo.JsonResult;
import com.wangshili.pojo.sys.entity.SysUser;

/**
 * 登录页控制层
 * @author wangshili
 *
 */
@Controller
public class LoginController {
	
	/**
	 * 登录校验
	 * @param isRememberMe
	 * @param username
	 * @param password
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/doLogin")
	public JsonResult doLogin(boolean isRememberMe,String username,String password) {
		/**获取subject*/
		Subject subject = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken(username, password);

		token.setRememberMe(isRememberMe);//记住我功能

		/**进行用户校验，shiro内部会自己调用我们继承的realm进行执行校验函数*/
		subject.login(token);
		return new JsonResult("ok");//成功的话就会跳转
		//return "starter";
	}
	
    @RequestMapping("doIndexUI")
    public String doIndexUI(Model model) {
     	/**
    	 * 也可以将数据存到域中通过thymeleaf取出，也可以使用shiro标签
    	 */ 
//        SysUser user=ShiroUtils.getUser();
//        model.addAttribute("user",user);
        return "starter";

    }
    
    /**
     * 跳转登录页
     * @return
     */
    @RequestMapping("/login")
    public String login() {
        return "login";

    }
}
